<?php 
session_start();
require_once ("model/m_movie.php");
require_once ("model/m_admin.php");
    $admin = New M_admin();
	$m_movie = new M_movie();
	//$user=$_SESSION['username'];
  if (isset($_POST['m'])  &&  $_POST['m']!=""){
		if ($_POST['m']=='add'){
		    $username=$_POST['u'];
            $password=$_POST['p'];
            $email=$_POST['e'];
            
            $r=$m_movie->CheckUserName($username);
            
            if ($r>0){
                 $result= array('msg'=> "0"); //Duplicate username
            
            }else{
                //Add new
                $m_movie->AddUser($username, $password, $email);
                
                //get userid 
                if ($result=$m_movie->login($username, $password)){
                    session_unregister("username");
                    session_unregister("userid");
                    
                    session_register("userid");
                    session_register("username");
                    
                    $rows=$result->FetchRow();
                                                        
                    $_SESSION['userid'] = $rows['ID'];
                    $_SESSION['username'] = $rows['username'];
                    
                    $m_movie->transferdata($rows['ID'],  $_SESSION['sess_id']);
                    
                    
                    if ($m_movie->getTotalSize() >0){
                        
                        $result= array('msg'=> "1",
                                        'confirm'=>"1");//Confirm : 1 goto page order
                    }else{
                         $result= array('msg'=> "1",
                                        'confirm'=>"0");//Confirm : 0 goto page index
                    }
                    
                    
                }else{
                    $result= array('msg'=> "2");//Not found
                }
                
                echo  json_encode( $result );//$html;
            }
        }else if ($_POST['m']=='delnews'){
            $id=$_POST['i'];
            $admin->delNews($id);
            echo "1";
                
        }else if ($_POST['m']=='news'){
            $order_by = $_POST['order'];
            $typeOrder=$_POST['typeOrder'];
            $title = $_POST['title'];
            $page=$_POST['page'];
            $rowPerPage=$_POST['rows'];
            $datefrom="";
            $dateto=""; 
            if ($_POST['from'] !=''){
                    list($day, $month, $year) = split('/', $_POST['from']);
                    $datefrom=$year."/".$month."/".$day;
                }
            if ($_POST['to'] !=''){
                list($day, $month, $year) = split('/', $_POST['to']);
                $dateto=$year."/".$month."/".$day;
            }  
            
            $result=$admin->getNews($title, $datefrom, $dateto,$order_by, $typeOrder, $page, $rowPerPage);
                
            $rows=$admin->getRowCount();
            $i=1 + (($page-1) * $rowPerPage);     
            
                if ($rows>0){
                    while($row = $result->FetchRow()){
                        If($i%2 <> 0){
                           $html .= "<tr class='row1'>";
                        } else{
                  
                            $html .= "<tr>";    
                        }
                        $html .= genTable3($row,$i)."</tr>";
                        $i++;
                    }
                }else{
                    $html="<tr>"
                            ."<td colspan='6' align='center' ><img src='images/info.png'/><b/></td>"
                        ."</tr>";
                }             
                 $ar_result=array('data'=>$html,
                                'rows'=>$rows,
                                'totalpage'=>$admin->getTotalPage());
                echo json_encode( $ar_result );//$html;
                
            
        }else if ($_POST['m']=='changeOrder'){
            
            $oid=$_POST['i'];
            $status=$_POST['s'];
            $admin->updateOrder($oid, $status);
            
            echo "1"; 
            
        }else if ($_POST['m']=='orderlist'){
            
            $order_by = $_POST['order'];
            $typeOrder=$_POST['typeOrder'];
            $status = $_POST['status'];
            $page=$_POST['page'];
            $rowPerPage=$_POST['rows'];
            $datefrom="";
            $dateto="";
            
            if ($_POST['from'] !=''){
                    list($day, $month, $year) = split('/', $_POST['from']);
                    $datefrom=$year."/".$month."/".$day;
                }
            if ($_POST['to'] !=''){
                list($day, $month, $year) = split('/', $_POST['to']);
                $dateto=$year."/".$month."/".$day;
            }  
 
            $result=$admin->getOrder($status, $datefrom, $dateto,$order_by, $typeOrder, $page, $rowPerPage);
                
            $rows=$admin->getRowCount();
            $i=1 + (($page-1) * $rowPerPage);     
            
                if ($rows>0){
                    while($row = $result->FetchRow()){
                        If($i%2 <> 0){
                           $html .= "<tr class='row1'>";
                        } else{
                  
                            $html .= "<tr>";    
                        }
                        $html .= genTable2($row,$i)."</tr>";
                        $i++;
                    }
                }else{
                    $html="<tr>"
                            ."<td colspan='10' align='center' ><img src='images/info.png'/><b/></td>"
                        ."</tr>";
                }             
                 $ar_result=array('data'=>$html,
                                'rows'=>$rows,
                                'totalpage'=>$admin->getTotalPage());
                echo json_encode( $ar_result );//$html;
                
        }else if ($_POST['m']=='login'){
            $username=$_POST['u'];
            $password=$_POST['p'];
            
            if ($result=$m_movie->login($username, $password)){
                session_unregister("username");
                session_unregister("userid");
                
                session_register("userid");
                session_register("username");
                
                $rows=$result->FetchRow();
                                
                $_SESSION['userid'] = $rows['ID'];
                $_SESSION['username'] = $rows['username'];
                
                
                $m_movie->transferdata($rows['ID'],  $_SESSION['sess_id']);
                
                 if ($m_movie->getTotalSize() >0){
                     $result= array('msg'=> "1",'confirm'=>"1");
                 }else{
                      $result= array('msg'=> "1",'confirm'=>"0");
                     
                 }
                
                
            }else{
                $result= array('msg'=> "0");
            }
            
            echo json_encode($result);
            
         }else if ($_POST['m']=='checkuser'){
                if(isset($_POST['username']) && $_POST['username']!="" && $_POST['password'] !="" && isset($_POST['password'])){ 
                    $name=$_POST['username'];
                    $password=$_POST['password'];
                    
                    $arr= $admin->checkUserLogin($name,$password);
                    
                    if ($arr['val']=="1"){
                        session_unregister("useradmin");
                        session_unregister("adminid");
                        
                        session_register("useradmin");
                        session_register("adminid");                     
                        
                        $_SESSION['useradmin'] = $arr['result']['username'];
                        $_SESSION['adminid'] = $arr['result']['ID'];        
                        
                        $result= array('msg'=> "1" );           
                        
                    }else if ($arr['val']=="2"){
                        $result= array('msg'=>"0");
                    }
                    echo json_encode($result);
                }else {
                    echo "error";
                }   
        }else if ($_POST['m']=='order'){
            $name=$_POST['name'];
            $addr = $_POST['addr'];
            $hdd = $_POST['hdd']; 
            $uid= $_SESSION['userid'];
            $size=$_POST['s'];
            $resutl=$m_movie->InsertOrder($name, $addr, $hdd, $uid, $size) ;
               
             echo json_encode(array('msg'=>1));
             
        }else if ($_POST['m']=='ac'){
            $mid=$_POST['mid'];
            $size=$_POST['size'];
            
            if (isset($_SESSION['userid'])){
                $uid= $_SESSION['userid'];
                $type=1;
            }else{
                $uid= $_SESSION['sess_id'];
                $type=0;     
            }
            $result=$m_movie->AddCart($uid, $mid, $size,$type);
            
            echo '$result';
            
        }else if ($_POST['m']=='rc'){
            $mid=$_POST['mid'];
           
            if (isset($_SESSION['userid'])){
                $uid= $_SESSION['userid'];
                $type=1;
            }else{
                $uid= $_SESSION['sess_id'];
                $type=0;     
            }
            $m_movie->removeCart($uid, $mid,$type);
            
            echo "remove";
            
        }else if ($_POST['m']=='dellist'){
             $mid=$_POST['i'];
             $uid= $_SESSION['userid'];
             $orderby = $_POST['order'];
             $ordertype = $_POST['typeOrder'];
                 
             $m_movie->removeCart($uid, $mid);
             
             $result=$m_movie->getMyList($orderby, $ordertype);
               $i=1;
               $totalGB=0;
               $html="";         
               while($row = $result->FetchRow()){
                   $html.="<tr>"
                        . "<td>". $i."</td>"
                        . "<td style='display:none;'>".$row['id']."</td>"
                        . "<td>" .$row['Category']."</td>"
                        . "<td>". $row['directoryname']."</td>"
                        . "<td>". $row['filesize_GB']."</td>"
                        . "<td>". $row['filetype']."</td>"
                        . "<td><img src='images/delete.png' class='btnDelete' title='ลบ' style='cursor:pointer'/></td>"
                        ."</tr>";
                   $i++;
                   $totalGB += $row['filesize_GB'];
                }
                if ($html ==""){
                    $html="<tr>"
                        ."<td colspan=7  style='text-align:center;' ><img src='./images/info.png'/><b/></td>"
                        ."</tr>";       
                }
               
               $ar_result=array('data'=>$html 
                                    ,'totalGB'=>$totalGB);
               
               echo json_encode( $ar_result );
                
        }else if ($_POST['m']=='getlist'){
           $orderby = $_POST['order'];
           $ordertype = $_POST['typeOrder'];    
 
           $result=$m_movie->getMyList($orderby, $ordertype);
           $i=1;
           $totalGB=$m_movie->getTotalSize();
           $html="";         
           while($row = $result->FetchRow()){
               $html.="<tr>"
                    . "<td>". $i."</td>"
                    . "<td style='display:none;'>".$row['id']."</td>"
                    . "<td>" .$row['Category']."</td>"
                    . "<td>". $row['directoryname']."</td>"
                    . "<td>". $row['filesize_GB']."</td>"
                    . "<td>". $row['filetype']."</td>"
                    . "<td><img src='images/delete.png' class='btnDelete' title='ลบ' style='cursor:pointer'/></td>"
                    ."</tr>";
               $i++;
               //$totalGB += $row['filesize_GB'];
            }
            if ($html ==""){
                $html="<tr>"
                    ."<td colspan=7  style='text-align:center;' ><img src='./images/info.png'/><b/></td>"
                    ."</tr>";       
            }
           
           $ar_result=array('data'=>$html 
                                ,'totalGB'=>$totalGB);
           
           echo json_encode( $ar_result );
               
        }else if ($_POST['m']=='search'){
            $name=$_POST['name'];
            $page=$_POST['page'];
            $pagelist=$_POST['pagelist'];
            
            $page=isset($page)?$page:1;
    
            $pagelist=isset($pagelist)?$pagelist:10;            
            
            $result=$m_movie->getAllVideoByName($name,$page,$pagelist);
            
            $rows=$m_movie->getRowCount();
            
            $i=1 + (($page-1) * $pagelist);
            
            $html="";
            $k=1;
             $totalGB=$m_movie->getTotalSize();
            
            if ($rows>0){
                while($row = $result->FetchRow()){
                    $html .= genTable($row,$k);
                    $k++; 
                 //  if ($row['value'] == 'Checked'){
                 //     $totalGB += $row['filesize_GB'];    
                 //   }
                   // $totalGB += $row['filesize_GB'];
                }
            }else{
                $html="<table width='100%'><tr>"
                    ."<td  align='center' ><img src='./images/info.png'/><br/>
                           <span class='Notfound'>Data Not Found</span></td>"
                    ."</tr></table>";
            }
            //Return result , rows , page
            $ar_result=array('data'=>$html,
                            'rows'=>$rows,
                            'totalGB'=>$totalGB,
                            'totalpage'=>$m_movie->getTotalPage());
            echo json_encode( $ar_result );//$html;
                        
                        			
		}else if ($_POST['m']== 'search_cat'){
			$code=$_POST['cat'];
			$page=$_POST['page'];
            $pagelist=$_POST['pagelist'];
	 
			$page=isset($page)?$page:1;
	
            $pagelist=isset($pagelist)?$pagelist:10;
    
			$result=$m_movie->getAllVideo($code,$page,$pagelist);
            
			$rows=$m_movie->getRowCount();
            
			$i=1 + (($page-1) * $pagelist);
			
			$html="";
			$k=1;
            $totalGB = $m_movie->getTotalSize();
			if ($rows>0){
				while($row = $result->FetchRow()){
 					$html .= genTable($row,$k);
 					//if ($row['value'] == 'Checked'){
 				//	  $totalGB += $row['filesize_GB'];    
 					//}
                    
                    $k++;
				}
			}else{
				$html="<table width='100%'><tr>"
					."<td  align='center' ><img src='./images/info.png'/><br/>
					       <span class='Notfound'>Data Not Found</span></td>"
					."</tr></table>";
			}
			//Return result , rows , page
			$ar_result=array('data'=>$html,
							'rows'=>$rows,
							'totalGB'=>$totalGB,
							'totalpage'=>$m_movie->getTotalPage());
			echo json_encode( $ar_result );//$html;
		}
	} 
 function genTable3($row,$k){
           $html="<td>".$k."</td>";
           $html.="<td style='display:none;'>".$row["id"]."</td>";
           
           $html.="<td>".$row["title"]."</td>";
           $html.="<td>".$row["createdDate"]."</td>";
            $html.="<td align='center'><a href='_news_details.php?m=edit&i=".$row['id']."'><img src='images/edit.png' style='cursor:pointer;' class='btnEdit' /></a></td>";
           $html.="<td align='center'><img src='images/delete.png' style='cursor:pointer;' class='btnDelete' /></td>";
  
         
             
     return $html;
 }
 function genTable2($row,$k){
           $html="<td>".$k."</td>";
           $html.="<td style='display:none;'>".$row["id"]."</td>";
           $html.="<td style='display:none;'>".$row["status"]."</td>";
           $html.="<td>".$row["order_id"]."</td>";
           $html.="<td>".$row["email"]."</td>";
           $html.="<td>".$row["sentto"]."<br>".$row["sentaddr"]."</td>";
           $html.="<td align='right'>".number_format( $row["totalsize"]  ,'2','.',',') ."</td>";
           $html.="<td><a href='javascript:void(0);' class='btnChangeStatus'>".$row["_status"]."</a></td>";
           $html.="<td>".$row["CreatedDate"]."</td>";
            $html.="<td align='center'><a href='export.php?id=".$row['id']."&name=".$row['order_id']."' target='_blank' '><img src='images/excel.png' style='cursor:pointer;' border=0 /></a></td>";  
           if ($row['status']==3)
             $html.="<td align='center'></td>";  
            else {
            	  $html.="<td align='center'><img src='images/delete.png' style='cursor:pointer;' class='btnDelete' /></td>";
            }
          
         
             
     return $html;
 }
 function genTable($row,$k){
		
        if($k%5==0){
            $class="productright";
        }else{
            $class="product";
        }
        
        $html="<span class='".$class."'>
                     <img src='http://copyhidef.com/getimg.php?src=".$row['id']."' class='coverImg' alt='' width='147' height='200' title='".$row['directoryname']."' />
                     <br />";
                     
        
            $html.= "<input name='cboRow[]' type='checkbox' ".$row['value']."  id='".$row['id']."' value='".$row['filesize_GB']."' />"; 
                                        
                    
        $html.="&nbsp;&nbsp;<span class='size'>".$row['filesize_GB']."</span> GB [".$row['fileType']."]</span>";
       
    	return $html;
	}

?>